package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/***
 * 角色操作
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    //新增角色
    @RequestMapping("/add.do")
    public Result add(@RequestBody Role role, Integer[] permissionIds) {
        try {
            roleService.add(role, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }

        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
    }


    //删除角色
    @RequestMapping("/delete.do")
    public Result findPage(Integer id) {
        try {
            roleService.delete(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    //角色分页查询
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        return pageResult;
    }

    //修改角色
    @RequestMapping("/edit.do")
    public Result edit(@RequestBody Role role, Integer[] permissionIds) {
        try {
            roleService.edit(role, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FALL);
        }

        return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
    }


    @RequestMapping("/findName.do")
    public Result findName(String name) {
        try {
            Role role = roleService.findName(name);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FALL);
        }
    }

    @RequestMapping("/findById.do")
    public Result findById(Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FALL);
        }
    }


    @RequestMapping("/findByRoleIdsByPermissionId.do")
    public Result findByRoleIdsByPermissionId(Integer id){
        try {
            List<Integer> permissionIds= roleService.findByRoleIdsByPermissionId(id);
            return new Result(true, MessageConstant.QUERY_PERMISSIN_SUCCESS, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSIN_FALL);
        }
    }

}
